package com.cy.module.dingtalk.mobility;

import com.cy.ext.plugin.route.ControllerBind;
import com.cy.model.Device;
import com.cy.model.SiteMapDevice;
import com.cy.module.WebApiController;

@ControllerBind(controllerKey = "/clmp/dingtalk/mobility/siteMapDevice")
public class SiteMapDeviceController extends WebApiController {

    @Override
    public void index() {
    }

    @Override
    public void update() {
        int id = getParaToInt();
        SiteMapDevice tempSiteMapDevice = getModel(SiteMapDevice.class);
        Device device = Device.dao.findById(tempSiteMapDevice.getDeviceId());
        if (device == null){
            renderFail("设备不存在");
            return;
        }
        boolean isEnabled = SiteMapDevice.dao.isEnabledDevice(tempSiteMapDevice.getSiteId(), tempSiteMapDevice.getDeviceId());
        if (!isEnabled){
            renderFail("设备正在门店使用中...");
            return;
        }
        SiteMapDevice siteMapDevice = SiteMapDevice.dao.findById(id);
        siteMapDevice.cover(tempSiteMapDevice);

        if (siteMapDevice.update()) {
            renderSuccess();
        } else {
            renderFail();
        }
    }

    @Override
    public void save() {
        SiteMapDevice siteMapDevice = getModel(SiteMapDevice.class);
        Device device = Device.dao.findById(siteMapDevice.getDeviceId());
        if (device == null){
            renderFail("设备不存在");
            return;
        }
        boolean isEnabled = SiteMapDevice.dao.isEnabledDevice(siteMapDevice.getSiteId(), siteMapDevice.getDeviceId());
        if (!isEnabled){
            renderFail("设备正在门店使用中...");
            return;
        }

        if (siteMapDevice.save()) {
            renderSuccess();
        } else {
            renderFail();
        }
    }

    @Override
    public void delete() {
        String[] ids = getPara().split("-");
        for (String id : ids) {
            SiteMapDevice.dao.deleteById(id);
        }
        renderSuccess();
    }
}
